/**
 * FileName: MqttAuthServiceImpl
 * Author: Wb
 * Date: 2025/9/26 9:20
 * Description:
 * History:
 */
package org.test.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.test.device.enty.Device;
import org.test.device.enty.Product;
import org.test.device.mapper.DeviceMapper;
import org.test.device.mapper.ProductMapper;
import org.test.device.service.MqttAuthService;
import org.test.device.utils.BusinessCode;
import org.test.device.utils.ResultData;

/**
 * 〈一句话功能简述〉<br>
 * 〈产品对接设备验证方式Service实现〉
 *
 * @author Wb
 * @create 2025/9/26
 * @since 1.0.0
 */
@Slf4j
@Service
public class MqttAuthServiceImpl implements MqttAuthService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    DeviceMapper deviceMapper;

    /**
     * @Author Wb
     * @Description 产品对接设备的校验
     * @Date 10:28 2025/9/26
     * @Param [clientId, username, password]
     * @Return boolean
     */
    @Override
    public ResultData authenticate(String clientId, String username, String password) {
        ResultData resultData = new ResultData();
        // 1. 验证客户端ID格式
        if (clientId == null || !clientId.contains("_")) {
            return new ResultData("客户端ID格式错误!", BusinessCode.CLIENT_ID_FORMAT_ERROR.name());
        }

        // 2. 解析客户端ID，提取产品标识和设备标识
        String[] parts = clientId.split("_", 2);
        String productKey = parts[0];
        String deviceKey = parts[1];

        // 3. 验证产品是否存在且已启用
        LambdaQueryWrapper<Product> prWrapper = new LambdaQueryWrapper<>();
        prWrapper.eq(Product::getCode, productKey);
        Product product = productMapper.selectOne(prWrapper);
        if (product == null) {
            return new ResultData("产品不存在", BusinessCode.PRODUCT_NOT_FOUND_OR_DISABLED.name());
        } else if (!(product.getStatus() == 1)) {
            return new ResultData("产品未启用", BusinessCode.PRODUCT_NOT_FOUND_OR_DISABLED.name());
        }

        // 4. 验证设备是否存在、属于该产品且已启用
        LambdaQueryWrapper<Device> deWrapper = new LambdaQueryWrapper<>();
        deWrapper.eq(Device::getCode, deviceKey);
        Device device = deviceMapper.selectOne(deWrapper);
        if (device == null) {
            return new ResultData("设备不存在或未启用", BusinessCode.DEVICE_NOT_FOUND_OR_DISABLED.name());
        }
        // else if (!device.getStatus().equals("2")) {
        //     return new ResultData("设备未启用", BusinessCode.DEVICE_NOT_FOUND_OR_DISABLED.name());
        // }

        // 5. 验证认证信息 - 优先使用设备单独设置的认证信息，否则使用产品的
        String authUsername = device.getName() != null ? device.getName() : product.getName();
        String authPassword = device.getAuthPassword() != null ? device.getAuthPassword() : product.getAuthPassword();

        // 6. 验证用户名密码
        if (password == null || !password.equals(authPassword)) {
            return new ResultData("密码错误", BusinessCode.AUTHENTICATION_FAILED.name());
        }
        return new ResultData("认证成功", BusinessCode.SUCCESS.name());
    }

}